package MediaOnline.Adapter;


import java.util.ArrayList;
import java.util.concurrent.ExecutionException;


import MediaOnline.Data.videoData;
import MediaOnline.GUI.R;
import MediaOnline.General.DownloadImages;
import MediaOnline.WebService.getVideoByPage;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewDebug.FlagToString;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;


public class VideoAdapter2 extends BaseAdapter {
	public Context context;
	//arrListFilm de gan vao gridview
	public static ArrayList<String> arrListFilm = new ArrayList<String>();
    
    public VideoAdapter2(Context ctx) {
        context=ctx;
    }

    public int getCount() {
        return arrListFilm.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    public void insertValue (){
    	
    }
    
    //Get list video su dung webservice va gan vao arrListFilm
    public void getListVideo(final Context x, int FilmCatID){
		getVideoByPage g = new getVideoByPage(x);
		videoData.lstVideo = new ArrayList<String>();
		videoData.lstVideoID = new ArrayList<String>();
		videoData.lstVideoLink=new ArrayList<String>();
		arrListFilm=new ArrayList<String>();
		videoData.lstThumb.clear();
		if(FilmCatID!=-1){
			g.filmCatID=FilmCatID;
		}
		g.execute();
    }
    
    //Lay danh sach downloaded video
    public void getDownloadedVideo(){
    	//arrListFilm.clear();
    	arrListFilm=videoData.lstFilmCat;
    }
    
    public View getView(final int position, View convertView, ViewGroup parent) {
    	View v = convertView;
        if (v == null) {
        	Log.d("Context::::", context.toString());
        	LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        	convertView = inflater.inflate(R.layout.filmsingle4, null);
        }
        
        //Hiển thị các thông tin
        //Lay hinh anh
        ImageView imageView = (ImageView)convertView.findViewById(R.id.filmsingle4_filmthumb);
        imageView.setScaleType(ScaleType.FIT_CENTER);
        imageView.setPadding(5, 5, 5, 5);
        //Tên phim chính
        TextView txtTenPhimChinh = (TextView) convertView.findViewById(R.id.filmsingle4_txtTenPhimChinh);
        txtTenPhimChinh.setText(videoData.lstVideo.get(position));
        //Tên phim phụ
        TextView txtTenPhimPhu = (TextView) convertView.findViewById(R.id.filmsingle4_txtTenPhimPhu);
        txtTenPhimPhu.setText(videoData.lstVideoTenPhimPhu.get(position));
        //Số tập
        String TenPhimPhu = videoData.lstVideoTenPhimPhu.get(position);
        if (videoData.lstSoTap.get(position)>1) {
        	TenPhimPhu+=" ("+videoData.lstSoTap.get(position)+" tập)";
		}
        txtTenPhimPhu.setText(TenPhimPhu);
        //Mô tả
        TextView txtMoTa = (TextView) convertView.findViewById(R.id.filmsingle4_txtMoTa);
        txtMoTa.setText(videoData.lstMoTa.get(position));
        //Đánh giá
        RatingBar rbRating = (RatingBar) convertView.findViewById(R.id.filmsingle4_rbDanhGia);
        rbRating.setRating((float)videoData.lstDanhGia.get(position));
        //Giá
        TextView txtGia = (TextView) convertView.findViewById(R.id.filmsingle4_txtGia);
        int gia=Integer.parseInt(videoData.lstGia.get(position));
        if (gia!=0) {
        	txtGia.setText(videoData.lstGia.get(position)+"đ");
		}
        else {
			txtGia.setText("Miễn phí");
		}
        
       //Nếu đã có hình ảnh thì load lên image view, không thì download về
        if (videoData.lstThumb.get(position)!=null){
        	//DownloadImages d = new DownloadImages(context, videoData.lstThumbUrl.get(position), imageView);
        	//d.execute();
        	imageView.setImageDrawable(videoData.lstThumb.get(position));
		}
		else{
			DownloadImages d = new DownloadImages(context, videoData.lstThumbUrl.get(position), imageView);
        	//d.execute();
        	try {
				videoData.lstThumb.set(position, d.execute().get());
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	//DownloadImagesAndSave ds = new DownloadImagesAndSave(context, videoData.lstThumbUrl.get(position), videoData.lstThumb.get(position));
			//imageView.setImageResource(R.drawable.movienoimg);
		}
        

        return convertView;

    }
}